package cn.ncupyh.soft.staticInnerClass;

import java.sql.*;

import static java.sql.DriverManager.getConnection;

public class JDBC {
    private static String url = "jdbc:mysql://127.0.0.1:3306/djx8120321008?serverTimezone=GMT%2B8&useSSL=false";
    private static String root = "root";
    private static String password = "djx020601";
    private static Connection conn = null;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 获取连接
    public static Connection getConn() {
        try {
            conn = getConnection(url, root, password);
        } catch (Exception e) {
            throw new RuntimeException("获得连接失败");
        }
        return conn;
    }

    // 释放连接
    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        rs = null;
        release(conn, pstmt);
    }

    public static void release(Connection conn, PreparedStatement pstmt) {
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        pstmt = null;
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            conn = null;
        }
    }
}
